package com.njxzc.service.before;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njxzc.common.http.ResponseResult;
import com.njxzc.entity.CartEntity;
import com.njxzc.entity.FocusEntity;
import com.njxzc.mapper.before.CartMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class CartServiceImpl extends ServiceImpl<CartMapper,CartEntity> implements CartService {
    @Override
    public ResponseResult<Map<String, Object>> addCart(CartEntity cartEntity) {
        Long goodsNum = lambdaQuery()
                .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId()).count();
        boolean s=false;
        if(goodsNum > 0){
            LambdaUpdateWrapper<CartEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(CartEntity::getShoppingnum,cartEntity.getShoppingnum()+1)
                    .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                    .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId());
            s=update(lambdaUpdateWrapper);

        }else{
            s=save(cartEntity);
        }
        if(s){
            return ResponseResult.getMessageResult(null,"A001");
        }else{
            return ResponseResult.getMessageResult(null,"A002");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Map<String, Object>> bupdate(CartEntity cartEntity) {

        List<Integer> bcid = cartEntity.getBcid();
        List<Integer> bshoppingnum = cartEntity.getBshoppingnum();
        ArrayList<CartEntity> cartEntities = new ArrayList<CartEntity>();
        for (int i = 0; i < bcid.size(); i++) {
            CartEntity cart = new CartEntity();
            cart.setId(bcid.get(i));
            cart.setShoppingnum(bshoppingnum.get(i));
            cartEntities.add(cart);
        }
        boolean b = updateBatchById(cartEntities);
        if(b){
            return ResponseResult.getMessageResult(null,"A001");
        }
        else{
            return ResponseResult.getMessageResult(null,"A002");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Map<String, Object>> clearCart(CartEntity cartEntity) {
        boolean isRemoved =
                lambdaQuery()
                .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                .;

        if (isRemoved) {
            return ResponseResult.getMessageResult(null, "A001"); // 操作成功
        } else {
            return ResponseResult.getMessageResult(null, "A002"); // 操作失败
        }
    }


    @Override
    public ResponseResult<Map<String, Object>> removeCart(CartEntity cartEntity) {
        boolean b = removeById(cartEntity.getId());
        if(b){
            return ResponseResult.getMessageResult(null,"A001");
        }
        else{
            return ResponseResult.getMessageResult(null,"A002");
        }
    }
}
